/**********************************************************************
 * SailmiPIM - modular cross-platform feature Personal Information Manager Tools.
 * Copyright(C) 2006-2014  Georg Rudoy
 *
 * Distributed under the Boost Software License, Version 1.0.
 *(See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
 **********************************************************************/

#include "stddatafiltermenucreator.h"
#include <QVariant>
#include <QMenu>
#include <sailmipim/iinfo.h>
#include <sailmipim/idatafilter.h>
#include <sailmipim/core/ientitymanager.h>
#include "util.h"

namespace SailmiPIM::Util
{
	namespace
	{
		template<typename T>
		void AddDatafilterMenuItem(const IDataFilter::FilterVariant& var, QMenu *menu, T actor)
		{
				const auto act = menu->addAction(var.Icon_, var.Name_);
				QObject::connect(act,
						&QAction::triggered,
						[var, actor]() mutable { actor(var); });
		}
	}

	StdDataFilterMenuCreator::StdDataFilterMenuCreator(const QVariant& dataVar, IEntityManager *em, QMenu *menu)
	: QObject(menu)
	{
		auto entity = MakeEntity(dataVar,
				QString(),
				FromUserInitiated | OnlyHandle,
				QStringLiteral("x-sailmipim/data-filter-request"));
		for(auto plugin : em->GetPossibleHandlers(entity))
		{
			auto ii = qobject_cast<IInfo*>(plugin);
			auto idf = qobject_cast<IDataFilter*>(plugin);
			if(!idf)
				continue;

			const auto& vars = idf->GetFilterVariants(dataVar);

			if(vars.isEmpty())
				continue;

			const auto actor = [this, entity, plugin, em](const IDataFilter::FilterVariant& var) mutable
					{
						entity.Additional_ [QStringLiteral("DataFilter")] = var.ID_;
						em->HandleEntity(entity, plugin);

						ChosenPlugin_ = qobject_cast<IInfo*>(plugin)->GetUniqueID();
						ChosenVariant_ = var.ID_;
					};

			if(vars.size() == 1)
				AddDatafilterMenuItem(vars.value(0), menu, actor);
			else
			{
				auto searchMenu = menu->addMenu(ii->GetIcon(), idf->GetFilterVerb());
				for(const auto& var : vars)
					AddDatafilterMenuItem(var, searchMenu, actor);
			}
		}
	}

	const QByteArray& StdDataFilterMenuCreator::GetChosenPlugin() const
	{
		return ChosenPlugin_;
	}

	const QByteArray& StdDataFilterMenuCreator::GetChosenVariant() const
	{
		return ChosenVariant_;
	}
}
